package com.yisu.expressway.utils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * Created by Administrator on 2016/10/19 0019.
 */
public class DateUtils {
    public static String getThisYear() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy",
                Locale.getDefault());
        return format.format(new Date());
    }

    public static String getThisMonth(){
        SimpleDateFormat format = new SimpleDateFormat("MM",
                Locale.getDefault());
        return format.format(new Date());
    }

    public static String getTimestamp(String time, String type) {
        SimpleDateFormat sdr = new SimpleDateFormat(type, Locale.getDefault());
        Date date;
        String times = null;
        try {
            date = sdr.parse(time);
            long l = date.getTime();
            times = String.valueOf(l);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return times;
    }

    public static String getMonthAndDay(String time){
        SimpleDateFormat formatter = new SimpleDateFormat("M月d日");

        String date = formatter.format(strToDate(time));
        return date;
    }

    public static String getWeek(String sdate) {
        // 再转换为时间
        Date date = strToDate(sdate);
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        // int hour=c.get(Calendar.DAY_OF_WEEK);
        // hour中存的就是星期几了，其范围 1~7
        // 1=星期日 7=星期六，其他类推
        return new SimpleDateFormat("EEEE").format(c.getTime());
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    public static String getDate(int periodDay) {

        Date todayDate = new Date();

        Calendar calendar = new GregorianCalendar();
        calendar.setTime(todayDate);
        calendar.add(Calendar.DATE, periodDay);
        Date newDate = calendar.getTime();

        SimpleDateFormat formatter = new SimpleDateFormat(todayDate.getYear() != newDate.getYear() ? "yy年M月d日" : "M月d日");
        String date = formatter.format(newDate);
        return date;
    }
}
